


/*
*************************************************************************
*西瑞克斯研发管理系统
*版 本: 1.0   
*日 期：02/08/2012    
*作 者： 王浩 QQ：873987060   
*************************************************************************   
*/  
package com.application.patent.dao;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;

import com.application.patent.entity.Patent;
import com.application.patent.struts.form.PatentForm;
import com.application.usertable.entity.Usertable;
import com.application.yfproject.entity.YfProject;
import com.manager.BaseDao;
import com.manager.commons.CommonUtil;
import com.manager.commons.CurrentUserUtils;
import com.manager.commons.DateUtil;
import com.manager.commons.dao.HibernateGenericDao;
import com.manager.commons.page.PageBean;
 /**
 * 
 * Description:PatentDAO<br />
 * 
 * Version:V0.1<br />
 * 
 * Author: 左磊 QQ：30575951   <br />
 * 
 * History: <br />
 * 
 * 02/08/2012 
 * 
 */
public class PatentDAO extends HibernateGenericDao<Patent>
{
        private static final Logger logger = Logger.getLogger(PatentDAO.class);

	private Patent patent;

	public Patent getPatent() {
		return this.patent;
	}

	public void setPatent(Patent patent) {
		this.patent = patent;
	}

	public Integer getAllCountPatent(PatentForm patentForm){
		//储存查询条件的buffer		
		List args = new ArrayList();
		StringBuffer buf = new StringBuffer(); 
		
		buf.append("SELECT COUNT(0) FROM patent t left join yf_project p on t.project_no = p.project_no  left join usertable u on p.project_manager = u.id left join department b on p.project_org = b.id WHERE 1=1  ");
		//查询字符串		
		buf = getStringBuffer(patentForm,buf,args);
		return super.getCount(buf.toString(),args.toArray());
	}
        
	public void deletePatents(PatentForm patentForm){    
		String a =patentForm.getIds().replaceAll(",","','");
    		String hql = " delete from Patent where id in  ( '" + a + "')";
		Session session = this.getSession();
		try
		{
			session.createQuery(hql).executeUpdate();
		}
		catch(RuntimeException re)
		{
		        logger.error("deletePatents failed");	
			throw re;
		}
		finally
		{
			if (session != null)
			{
				releaseSession(session);
			}
		}
       }

	public List searchPatent(final PatentForm patentForm,final PageBean pageBean){  
	    
			List args = new ArrayList();
			StringBuffer buf = new StringBuffer(); 
			buf.append("SELECT t.*,t.patent_no as patentno,t.project_no as projectNo,u.rname as uname,p.project_org as projectOrg,p.project_name as projectName,b.name as orgname FROM " +
					"patent t left join yf_project p on t.project_no = p.project_no  left join usertable u on p.project_manager = u.id left join department b on p.project_org = b.id WHERE 1=1  ");
			buf = getStringBuffer(patentForm,buf,args);
			buf.append(" order by t.createDate Desc ");
			return getPageInfo(pageBean,buf.toString(),args);
	}

	public List searchPatent(final PatentForm patentForm){  
		List args = new ArrayList();
		StringBuffer buf = new StringBuffer(); 
		buf.append("SELECT t.*,t.patent_no as patentno,t.project_no as projectNo,u.rname as uname,p.project_org as projectOrg,p.project_name as projectName,b.name as orgname   FROM " +
				"patent t left join yf_project p on t.project_no = p.project_no  left join usertable u on p.project_manager = u.id left join department b on p.project_org = b.id WHERE 1=1  ");
		buf = getStringBuffer(patentForm,buf,args);
		buf.append(" order by t.createDate Desc ");
		return getPageInfo(null,buf.toString(),args);
	}
	
	/**
	 * 查询条件的封装 
	 * @param patentForm 
	 * @return
	 */
	private StringBuffer getStringBuffer(PatentForm patentForm,StringBuffer buf,List args){
		/* 需要加入查询条件时封装
		*/
         if(StringUtils.isNotBlank(patentForm.getId())){
           	 buf.append(" and t.id = '" + patentForm.getId().trim() + "'");
    		}
         if(StringUtils.isNotBlank(patentForm.getProjectNo())){
           	 buf.append(" and t.project_no = '" + patentForm.getProjectNo().trim() + "'");
    		}
         if(StringUtils.isNotBlank(patentForm.getProjectOrg())){
           	 buf.append(" and p.project_org = '" + patentForm.getProjectOrg().trim() + "'");
    		}
         if(StringUtils.isNotBlank(patentForm.getProjectName())){
           	 buf.append(" and p.project_name like '%" + patentForm.getProjectName().trim() + "%'");
    		}
         if(StringUtils.isNotBlank(patentForm.getAccredit())){
           	 buf.append(" and t.accredit = '" + patentForm.getAccredit().trim() + "'");
    		}
         if(StringUtils.isNotBlank(patentForm.getAccredittype())){
           	 buf.append(" and t.accredittype = '" + patentForm.getAccredittype().trim() + "'");
    		}
         if(StringUtils.isNotBlank(patentForm.getPatentno())){
           	 buf.append(" and t.patent_no like '%" + patentForm.getPatentno().trim() + "%'");
    		}
         if(StringUtils.isNotBlank(patentForm.getUname())){
           	 buf.append(" and u.rname like '%" + patentForm.getUname().trim() + "%'");
    		}
 		if (StringUtils.isNotBlank(patentForm.getOperateid())) {
			buf.append(" and t.operateid = '" + patentForm.getOperateid()+"'");
		}
// 		buf = CommonUtil.filterData(buf,args);
        return buf;
	}
}
	